package com.cloudpolice.equipment.controller;

import com.cloudpolice.equipment.model.EquipmentMessageModel;
import com.cloudpolice.equipment.model.InPutParam.EquipmentMessageSelectInParam;
import com.cloudpolice.equipment.service.EquipmentMessageService;
import com.cloudpolice.equipment.unit.GetPropertiesClass;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.ResultVO;
import lh.model.ResultVOPage;
import lh.toolclass.LhClass;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：flyman5，前端工程师：孙耀
 * @create 2020-04-01 22:33
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/equipmentMessageController")
@Api(value = "设备粒子层", description = "门禁机消息与通知")
public class EquipmentMessageController {
    @Autowired
    EquipmentMessageService equipmentMessageService;
    @Autowired
    GetPropertiesClass getPropertiesClass;

    /**
     * 根据消息ID得到消息与通知信息，方法ID：SE202004020015307960B88322AD120
     *
     * @param id 表vou_EquipmentMessage,字段名id:
     * @return 设备基本信息
     */
    @ApiOperation(value = "根据消息ID得到消息与通知信息,方法ID：SE202004020015307960B88322AD120", notes = "设备基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true)
    })
    @PostMapping("/selectEquipmentMessageById")
    public ResultVO selectEquipmentMessageById(@RequestParam(value = "id") String id) {
        id = id == null ? id : id.trim();
        return ResultStruct.success(equipmentMessageService.selectEquipmentMessageById(id));
    }

    /**
     * 根据社区ID或区域ID得到消息与通知信息，方法ID：SE2020040200091279772685E0166F9
     *
     * @param id          表vou_EquipmentMessage,字段名id:
     * @param communityId 表vou_EquipmentMessage,字段名communityId:社区ID
     * @param periodId    表vou_EquipmentMessage,字段名periodId:社区区域ID
     * @param page        当前页数
     * @param limit       每页条数
     * @return 设备基本信息
     */
    @ApiOperation(value = "根据社区ID或区域ID得到消息与通知信息,方法ID：SE2020040200091279772685E0166F9", notes = "设备基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String"),
            @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true),
            @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/selectEquipmentMessageByCom")
    public ResultVOPage selectEquipmentMessageByCom(@RequestParam(value = "id", required = false) String id
            , @RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId", required = false) String periodId
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit) {
        id = id == null ? id : id.trim();
        communityId = communityId == null ? communityId : communityId.trim();
        periodId = periodId == null ? periodId : periodId.trim();

        EquipmentMessageSelectInParam equipmentMessageSelectInParam = new EquipmentMessageSelectInParam();
        equipmentMessageSelectInParam.setId(id);
        equipmentMessageSelectInParam.setCommunityId(communityId);
        equipmentMessageSelectInParam.setPeriodId(periodId);

        PageHelper.startPage(page, limit);
        List<EquipmentMessageModel> equipmentMessageModels = equipmentMessageService.selectEquipmentMessageByCom(equipmentMessageSelectInParam);
        PageInfo pageInfo = new PageInfo<>(equipmentMessageModels, limit);
        return ResultStruct.successPage(equipmentMessageModels, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }

    /**
     * 修改门禁机消息与通知，方法ID：UP20200401232747641D56307B2F8CC
     *
     * @param id          , Where字段
     * @param communityId 社区ID
     * @param periodId    社区区域ID
     * @param message     消息
     * @param messageType 消息类型（0：一般消息、1：通知）
     * @param createName  发布者名称
     * @param createTime  通知日期
     * @param audioSign   是否审批通过
     * @param sortNo      出现顺序
     * @param beginDate   发布开始日期（不包括当天）
     * @param endDate     发布结束日期（不包括当天）
     * @return 影响条件
     */
    @ApiOperation(value = "修改门禁机消息与通知,方法ID：UP20200401232747641D56307B2F8CC", notes = "影响条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true)
            , @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String")
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String")
            , @ApiImplicitParam(name = "message", value = "消息", dataType = "String")
            , @ApiImplicitParam(name = "messageType", value = "消息类型（0：一般消息、1：通知）", dataType = "Integer")
            , @ApiImplicitParam(name = "createName", value = "发布者名称", dataType = "String")
            , @ApiImplicitParam(name = "createTime", value = "通知日期", dataType = "Date")
            , @ApiImplicitParam(name = "audioSign", value = "是否审批通过", dataType = "Boolean")
            , @ApiImplicitParam(name = "sortNo", value = "出现顺序", dataType = "Integer")
            , @ApiImplicitParam(name = "beginDate", value = "发布开始日期（不包括当天）", dataType = "Date")
            , @ApiImplicitParam(name = "endDate", value = "发布结束日期（不包括当天）", dataType = "Date")
            , @ApiImplicitParam(name = "colorR", value = "R颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorG", value = "G颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorB", value = "B颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "fontSize", value = "文字大小", dataType = "String", defaultValue = "15", required = true)
    })
    @PostMapping("/updateEquipmentMessage")
    public int updateEquipmentMessage(@RequestParam(value = "id") String id
            , @RequestParam(value = "communityId", required = false) String communityId
            , @RequestParam(value = "periodId", required = false) String periodId
            , @RequestParam(value = "message", required = false) String message
            , @RequestParam(value = "messageType", required = false) int messageType
            , @RequestParam(value = "createName", required = false) String createName
            , @RequestParam(value = "createTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime
            , @RequestParam(value = "audioSign", required = false) boolean audioSign
            , @RequestParam(value = "sortNo", required = false) int sortNo
            , @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate
            , @RequestParam(value = "colorR", defaultValue = "-1") int colorR
            , @RequestParam(value = "colorG", defaultValue = "-1") int colorG
            , @RequestParam(value = "colorB", defaultValue = "-1") int colorB
            , @RequestParam(value = "fontSize", defaultValue = "-1") int fontSize
    ) {
        id = id == null ? id : id.trim();
        communityId = communityId == null ? communityId : communityId.trim();
        periodId = periodId == null ? periodId : periodId.trim();
        message = message == null ? message : message.trim();
        createName = createName == null ? createName : createName.trim();

        EquipmentMessageModel equipmentMessageModel = new EquipmentMessageModel();
        equipmentMessageModel.setId(id);
        equipmentMessageModel.setCommunityId(communityId);
        equipmentMessageModel.setPeriodId(periodId);
        equipmentMessageModel.setMessage(message);
        if (messageType > -1)
            equipmentMessageModel.setMessageType(messageType);
        if (sortNo > -1) {
            equipmentMessageModel.setSortNo(sortNo);
        }
        equipmentMessageModel.setCreateTime(createTime);
        equipmentMessageModel.setCreateName(createName);
        equipmentMessageModel.setAudioSign(audioSign);
        equipmentMessageModel.setBeginDate(beginDate);
        equipmentMessageModel.setEndDate(endDate);
        if (colorR > -1)
            equipmentMessageModel.setColorR(colorR);
        if (colorB > -1)
            equipmentMessageModel.setColorG(colorB);
        if (colorG > -1)
            equipmentMessageModel.setColorB(colorG);
        if (fontSize > -1)
            equipmentMessageModel.setFontSize(fontSize);
        int updateCount = equipmentMessageService.updateEquipmentMessage(equipmentMessageModel);

        return updateCount;
    }

    /**
     * 自动删除过期的门禁机消息与通知，方法ID：DE202004012319246865629E542A7DE
     *
     * @return 影响条件
     */
    @ApiOperation(value = "删除门禁机消息与通知,方法ID：DE202004012319246865629E542A7DE", notes = "影响条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String")
    })
    @PostMapping("/deleteEquipmentMessageAuto")
    public int deleteEquipmentMessageAuto() {
        EquipmentMessageModel equipmentMessageModel = new EquipmentMessageModel();
        equipmentMessageModel.setEndDate(new Date());
        return equipmentMessageService.deleteEquipmentMessageAuto(equipmentMessageModel);
    }

    /**
     * 删除门禁机消息与通知，方法ID：DE202004012319246865629E542A7DE
     *
     * @param id
     * @return 影响条件
     */
    @ApiOperation(value = "删除门禁机消息与通知,方法ID：DE202004012319246865629E542A7DE", notes = "影响条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String")
    })
    @PostMapping("/deleteEquipmentMessage")
    public int deleteEquipmentMessage(@RequestParam(value = "id", required = false) String id) {
        id = id == null ? id : id.trim();

        EquipmentMessageModel equipmentMessageModel = new EquipmentMessageModel();
        equipmentMessageModel.setId(id);
        int updateCount = equipmentMessageService.deleteEquipmentMessage(equipmentMessageModel);

        return updateCount;
    }

    /**
     * 增加门禁机消息与通知，方法ID：IN202004012252589001A8DE9D9BFA9
     *
     * @param communityId 社区ID
     * @param periodId    社区区域ID
     * @param message     消息
     * @param messageType 消息类型（0：一般消息、1：通知）
     * @param createName  发布者名称
     * @param createTime  通知日期
     * @param audioSign   是否审批通过
     * @param sortNo      出现顺序
     * @param beginDate   发布开始日期（不包括当天）
     * @param endDate     发布结束日期（不包括当天）
     * @return 影响条件
     */
    @ApiOperation(value = "增加门禁机消息与通知,方法ID：IN202004012252589001A8DE9D9BFA9", notes = "影响条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "message", value = "消息", dataType = "String", required = true)
            , @ApiImplicitParam(name = "messageType", value = "消息类型（0：一般消息、1：通知）", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createName", value = "发布者名称", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createTime", value = "通知日期", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "audioSign", value = "是否审批通过", dataType = "Boolean", required = true)
            , @ApiImplicitParam(name = "sortNo", value = "出现顺序", dataType = "String", required = true)
            , @ApiImplicitParam(name = "beginDate", value = "发布开始日期（不包括当天）", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "endDate", value = "发布结束日期（不包括当天）", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "colorR", value = "R颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorG", value = "G颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "colorB", value = "B颜色值", dataType = "String", defaultValue = "255", required = true)
            , @ApiImplicitParam(name = "fontSize", value = "文字大小", dataType = "String", defaultValue = "15", required = true)
    })
    @PostMapping("/insertEquipmentMessage")
    public int insertEquipmentMessage(@RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId") String periodId
            , @RequestParam(value = "message") String message
            , @RequestParam(value = "messageType") int messageType
            , @RequestParam(value = "createName") String createName
            , @RequestParam(value = "createTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime
            , @RequestParam(value = "audioSign", defaultValue = "1") boolean audioSign
            , @RequestParam(value = "sortNo") int sortNo
            , @RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate
            , @RequestParam(value = "colorR", defaultValue = "-1") int colorR
            , @RequestParam(value = "colorG", defaultValue = "-1") int colorG
            , @RequestParam(value = "colorB", defaultValue = "-1") int colorB
            , @RequestParam(value = "fontSize", defaultValue = "-1") int fontSize
    ) {
        communityId = communityId == null ? communityId : communityId.trim();
        periodId = periodId == null ? periodId : periodId.trim();
        message = message == null ? message : message.trim();
        createName = createName == null ? createName : createName.trim();

        EquipmentMessageModel equipmentMessageModel = new EquipmentMessageModel();
        String mainKey = LhClass.getPicturyKey(Short.valueOf(getPropertiesClass.getPort()));//这里引用架包中的生成主键方法
        equipmentMessageModel.setId(mainKey);
        equipmentMessageModel.setCommunityId(communityId);
        equipmentMessageModel.setPeriodId(periodId);
        equipmentMessageModel.setMessage(message);
        equipmentMessageModel.setMessageType(messageType);
        equipmentMessageModel.setCreateName(createName);
        equipmentMessageModel.setCreateTime(createTime);
        equipmentMessageModel.setAudioSign(audioSign);
        equipmentMessageModel.setSortNo(sortNo);
        equipmentMessageModel.setBeginDate(beginDate);
        equipmentMessageModel.setEndDate(endDate);
        if (colorR > -1)
            equipmentMessageModel.setColorR(colorR);
        if (colorB > -1)
            equipmentMessageModel.setColorG(colorB);
        if (colorG > -1)
            equipmentMessageModel.setColorB(colorG);
        if (fontSize > -1)
            equipmentMessageModel.setFontSize(fontSize);
        int resultCount = equipmentMessageService.insertEquipmentMessage(equipmentMessageModel);

        return resultCount;
    }

    /**
     * 根据设备SN号得到消息与通知信息，方法ID：SE20200401214022506AF10BC0583F0
     *
     * @param communityId 表vou_EquipmentMessage,字段名communityId:社区ID
     * @param periodId    表vou_EquipmentMessage,字段名periodId:社区区域ID
     * @return 设备基本信息
     */
    @ApiOperation(value = "根据设备SN号得到消息与通知信息,方法ID：SE20200401214022506AF10BC0583F0", notes = "设备基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true),
            @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String", required = true)
    })
    @PostMapping("/selectEquipmentMessageBySn")
    public ResultVO selectEquipmentMessageBySn(@RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId") String periodId) {
        communityId = communityId == null ? communityId : communityId.trim();
        periodId = periodId == null ? periodId : periodId.trim();
        final Date date = new Date();
        EquipmentMessageSelectInParam equipmentMessageSelectInParam = new EquipmentMessageSelectInParam();
        equipmentMessageSelectInParam.setCommunityId(communityId);
        equipmentMessageSelectInParam.setPeriodId(periodId);
        equipmentMessageSelectInParam.setBeginDate(date);
        equipmentMessageSelectInParam.setEndDate(date);
        final List<EquipmentMessageModel> equipmentMessageModels = equipmentMessageService.selectEquipmentMessageBySn(equipmentMessageSelectInParam);
        return ResultStruct.success(equipmentMessageModels, equipmentMessageModels.size());
    }
}
